<template>
	<view>
		<view class="content">
			<view class="row">
				<view class="nominal">
					收件人
				</view>
				<view class="input">
					<input placeholder="请输入收件人姓名" type="text" v-model="address.name" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					电话号码
				</view>
				<view class="input">
					<input placeholder="请输入收件人电话号码" type="text" v-model="address.telephone" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					所在地区
				</view>
				<view class="input" @tap="chooseCity">
					<text v-if="address.province != ''">
						{{address.province_code}}-{{address.city_code}}-{{address.area_code}}
					</text>
					<text v-else>
						请选择地址
					</text>
				</view>

			</view>
			<view class="row">
				<view class="nominal">
					详细地址
				</view>
				<view class="input">
					<textarea v-model="address.address" auto-height="true" placeholder="输入详细地址"></textarea>
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					设置默认地址
				</view>
				<view class="input switch">
					<switch color="#e81311" :checked="address.default==1?true:false" @change="isDefaultChange" />
				</view>
			</view>
			<view class="row" v-if="editType=='edit'" @tap="del">
				<view class="del">
					删除收货地址
				</view>
			</view>
		</view>
		<view class="save" @tap="save">
			<view class="btn">
				保存地址
			</view>
		</view>
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValue" @onCancel="onCancel"
		 @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	import api from '@/comon/api.js'
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue';
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				editType: 'edit',
				themeColor: '#007AFF',
				cityPickerValue: [0, 0, 1],
				address: {
					address: "",
					area_code: "",
					city_code: "",
					default: 0,
					name: "",
					province_code: "",
					telephone: "",
					user_id: ""
				}
			};
		},
		onLoad(e) {
			//获取传递过来的参数
			this.editType = e.type;
			if (e.type == 'edit') {
				uni.getStorage({
					key: 'address',
					success: (e) => {
						this.address = e.data
					}
				})
			}
			// #ifdef MP-ALIPAY
			my.setNavigationBar({
				backgroundColor:'#f5f5f5'
			})
			// #endif
		},
		methods: {
			onCancel(e) {
				console.log(e)
			},
			chooseCity() {
				this.$refs.mpvueCityPicker.show()
			},
			onConfirm(e) {
				e = e.label.split('-');
				this.address.province_code = e[0];
				this.address.city_code = e[1];
				this.address.area_code = e[2];
			},
			isDefaultChange(e) {
				this.address.default = e.detail.value == true ? 1 : 0;
			},
			async del() {
				this.address.type = 3
				let res = await api.uniCloudApi(api.publicFun.address,this.address);
				if(res){
					this.showToast("删除成功")
					setTimeout(()=>{
						uni.navigateBack({})
					},500)
				}
			},
			async save() {
				let userinfo = uni.getStorageSync('userInfos');
				let res = '';
				if (this.address.name.length < 2) {
					this.showToast('收件人最少两位长度')
					return
				}
				let phoneReg = /^1[1-9][0-9]\d{8}$/;
				if (!phoneReg.test(this.address.telephone)) {
					this.showToast('电话号码不合法');
					return
				}
				if (this.address.province_code == '') {
					this.showToast('请选择所在地区');
					return
				}
				if (this.address.address == '') {
					this.showToast('请输入详细地址');
					return
				}
				if (this.editType == 'add') {
					this.address.user_id = userinfo._id;
					this.address.type = 2
					delete this.address._id
					res = await api.uniCloudApi(api.publicFun.address,this.address);
				}else {
					this.address.type = 1
					res = await api.uniCloudApi(api.publicFun.address,this.address);
				}
				if(res){
					this.showToast(this.editType == 'add'?'地址添加成功':'地址修改成功')
					setTimeout(()=>{
						uni.navigateBack({})
					},500)
				}
			},
			showToast(msg) {
				uni.showToast({
					icon: 'none',
					title: msg
				})
			}
		}
	};
</script>
<style lang="scss">
	page{
		background-color: #f5f5f5;
	}
	.save {
		view {
			display: flex;
		}

		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;

		.btn {
			box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			background-color: #e81311;
			color: #fff;
			justify-content: center;
			align-items: center;

			.icon {
				height: 80upx;
				color: #fff;
				font-size: 30upx;
				justify-content: center;
				align-items: center;
			}

			font-size: 30upx;
		}
	}

	.content {
		display: flex;
		flex-wrap: wrap;

		view {
			display: flex;
		}

		.row {
			width: 94%;

			margin: 0 3%;
			border-top: solid 1upx #eee;

			.nominal {
				width: 30%;
				height: 120upx;
				font-weight: 200;
				font-size: 30upx;
				align-items: center;
			}

			.input {
				width: 70%;
				padding: 20upx 0;
				align-items: center;
				font-size: 30upx;

				&.switch {
					justify-content: flex-end;
				}

				.textarea {
					margin: 20upx 0;
					min-height: 120upx;
				}

				/* #ifdef MP-ALIPAY */
				input,
				textarea{
					background-color: #f5f5f5;
				}
				/* #endif */
			}

			.del {
				width: 100%;
				height: 100upx;
				justify-content: center;
				align-items: center;
				font-size: 36upx;
				color: #FFFFFF;
				background-color: #e81311;
				border-bottom: solid 1upx #eee;
			}
		}
	}
</style>
